//app.js
let diy = require('./js/diy.js');
var fcbuf = require('./fcbuf');
App({
    onLaunch: function () {
        let that = this;
        swan.setStorageSync("url", this.globalData.url);
        swan.setStorageSync("iiid", 0);
        if(!swan.getStorageSync('area')){
            swan.setStorageSync("area", '无锡');
        }
        this.updateVersion();
    },
    onShow: function (e) {
    },
    rq: function (url, data, load, method, callback) {
        return diy.rq(url, data, load, method, callback);
    },

    compareVersion(v1, v2) {
        v1 = v1.split('.')
        v2 = v2.split('.')
        var len = Math.max(v1.length, v2.length)
        while (v1.length < len) {
            v1.push('0')
        }
        while (v2.length < len) {
            v2.push('0')
        }
        for (var i = 0; i < len; i++) {
            var num1 = parseInt(v1[i])
            var num2 = parseInt(v2[i])
            if (num1 > num2) {
                return 1
            } else if (num1 < num2) {
                return -1
            }
        }
        return 0
    },
    updateVersion: function (n) {
        let that = this;
        swan.getSystemInfo({
            success: (res) => {
                let SDKVersion = res.SDKVersion;
                let bool = that.compareVersion(SDKVersion, '1.12.43');
                if (bool == 1) {
                    const updateManager = swan.getUpdateManager();
                    console.log(updateManager);
                    updateManager.onCheckForUpdate(function (res) {
                        // 请求完新版本信息的回调
                        console.log(res.hasUpdate);
                    });
                    updateManager.onUpdateReady(function (res) {
                        swan.showModal({
                            title: '更新提示',
                            content: '新版本已经准备好，是否马上重启小程序？',
                            success: function (res) {
                                if (res.confirm) {
                                    // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                                    updateManager.applyUpdate();
                                }
                            }
                        });
                    });
                    updateManager.onUpdateFailed(function () {
                        // 新的版本下载失败
                    });
                }
            }
        });
    },
    globalData: {
        url: 'https://jiaoyu.jsbestop.com/newPhp/index.php/Api',
        imageurl: 'https://jiaoyu.jsbestop.com/newPhp/uploads/',
        staticImageurl: "https://jiaoyu.jsbestop.com/newPhp/static/",
        config: [],
        banner: ''
    }
});